#include "datadispose.h"

QList<QTcpSocket*> socket_list;

usrdata m_usrdate[USERSMAXNUM];
int m_nUsrNums[USERSMAXNUM];
int m_nUsrNum = 2;
char m_cGobangRoom[GOBANGMAXNUM][15][15];
int m_nUserRooms[GOBANGMAXNUM][30];

//处理读取数据
void DisposeReadData(QTcpSocket *tcpsoc, QString readdata)
{
    QString keywords = readdata.section('@', 0, 0);
    if (keywords == "OK")
    {
        tcpsoc->write("OK@");//senddata(tcpsoc, "OK@");
    }
    else if (keywords == "text")
    {
        QString backdata = readdata.mid(keywords.length() + 1);
        for (int i = 0; i < USERSMAXNUM; i++)
        {
            if (m_usrdate[i].usrname == backdata.section(' ', 0, 0))
            {
                QString sendstr = QString("usrnum@");
                sendstr.append(QString("%1 %2").arg(i).arg(m_usrdate[i].number));
                tcpsoc->write(sendstr.toUtf8().data());
            }
        }
    }
    else if (keywords == "searchuser")
    {
        QString sendstr = QString("allusers@");
        for (int i = 0; i < USERSMAXNUM; i++)
        {
            if (m_usrdate[i].usrname.isEmpty() == false)
            {
                //printf("%s ", usrdat[i].usrname.toUtf8().data());
                sendstr.append(" ");
                sendstr.append(m_usrdate[i].usrname);
                return ;
            }
        }
        tcpsoc->write(sendstr.toUtf8().data());//senddata(tcpsoc, sendstr);
    }
    else if (keywords == "register")//用户注册
    {
        QString backdata = readdata.mid(keywords.length() + 1);
        //qDebug() << "backdata" << backdata;
        for (int i = 0; i < USERSMAXNUM; i++)
        {
            if (m_usrdate[i].usrname == backdata.section(' ', 0, 0))
            {
                tcpsoc->write("namesame@");//senddata(tcpsoc, "namesame@");//注册失败，用户名重复
                return ;
            }
        }
        /*
        int inum;
        for (int i = 0; i < USERSMAXNUM; i++)
        {
            if (m_nUsrNums[i] == 0)
            {
                m_nUsrNums[i] = m_nUsrNum;
                inum = i;
                qDebug() << "usrnum:" << i << ", m_nUsrNum:" << m_nUsrNum;
                break ;
            }
        }
        */
        for (int i = 0; i < USERSMAXNUM; i++)
        {
            if (m_usrdate[i].usrname.isEmpty())
            {
                m_nUsrNum = i;
                break;
            }
        }
        m_usrdate[m_nUsrNum].usrname = backdata.section(' ', 0, 0);
        m_usrdate[m_nUsrNum].passwd = backdata.section(' ', 1, 1);
        m_usrdate[m_nUsrNum].number = m_nUsrNum;
        //m_usrdate[m_nUsrNum].usrnumvalue = inum;
        m_nUsrNum++;
        SaveUserData();
        for (int i = 0; i < USERSMAXNUM; i++)
        {
            if (m_usrdate[i].usrname == backdata.section(' ', 0, 0))
            {
                //printf("%s ", usrdat[i].usrname.toUtf8().data());
                tcpsoc->write("registerOK@");//senddata(tcpsoc, "registerOK@");

                return ;
            }
        }
        //注册成功，发送OK
    }
    else if (keywords == "login")//用户登录
    {
        QString backdata = readdata.mid(keywords.length() + 1);
        if (backdata.section(' ', 0, 0).isEmpty() || backdata.section(' ', 1, 1).isEmpty())
        {
            tcpsoc->write("loginnoname@");
            return ;
        }
        for (int i = 0; i < USERSMAXNUM; i++)
        {
            if (m_usrdate[i].usrname == backdata.section(' ', 0, 0))
            {
                if (m_usrdate[i].passwd == backdata.section(' ', 1, 1))
                {
                    if (m_usrdate[i].socket == 0 || m_usrdate[i].socket->peerAddress() != tcpsoc->peerAddress())
                    {
                        m_usrdate[i].socket = tcpsoc;
                        tcpsoc->write("loginOK@");//senddata(tcpsoc, "loginOK@");
                        return ;
                    }
                    else
                    {
                        tcpsoc->write("loginyet@");//senddata(tcpsoc, "loginyet@");
                        return ;
                    }
                }
                else
                {
                    tcpsoc->write("loginnopass@");//senddata(tcpsoc, "loginnopass@");
                    return ;
                }
            }
        }
        tcpsoc->write("loginnoname@");//senddata(client_socket, "loginnoname@");
        return ;
    }
    else if (keywords == "readuserdata")//读取用户信息
    {
        QString backdata = readdata.mid(keywords.length() + 1);
        for (int i = 0; i < USERSMAXNUM; i++)
        {
            if (m_usrdate[i].usrname == backdata)
            {
                QString sendstr = QString("userdata@%1 %2 %3 %4 %5 %6").arg(m_usrdate[i].usrname).arg(m_usrdate[i].number).arg(m_usrdate[i].score).arg(m_usrdate[i].gobangnum).arg(m_usrdate[i].winnum).arg(m_usrdate[i].lossnum);
                tcpsoc->write(sendstr.toUtf8().data());//senddata(tcpsoc, sendstr);
                return ;
            }
        }
    }
    else if (keywords == "readinform")//读取通知
    {
        QString backdata = readdata.mid(keywords.length() + 1);
        for (int i = 0; i < USERSMAXNUM; i++)
        {
            if (m_usrdate[i].usrname == backdata)
            {
                QString sendstr = QString("sendinform@%1").arg(m_usrdate[i].inform);
                tcpsoc->write(sendstr.toUtf8().data());//senddata(tcpsoc, sendtr.toUtf8());
                return ;
            }
        }
    }
    else if (keywords == "readfriends")//读取好友
    {
        QString backdata = readdata.mid(keywords.length() + 1);
        for (int i = 0; i < USERSMAXNUM; i++)
        {
            if (m_usrdate[i].usrname == backdata)
            {
                QString sendtr = QString("sendfriends@%1").arg(m_usrdate[i].friends);
                tcpsoc->write(sendtr.toUtf8().data());//senddata(tcpsoc, sendtr.toUtf8());
                return ;
            }
        }
    }
    else if (keywords == "changename")//修改用户名
    {
        QString backdata = readdata.mid(keywords.length() + 1);
        for (int i = 0; i < USERSMAXNUM; i++)
        {
            if (m_usrdate[i].usrname == backdata.section(' ', 0, 0))
            {
                m_usrdate[i].usrname = backdata.section(' ', 1, 1);
                SaveUserData();
                tcpsoc->write("changenameOK@");//senddata(tcpsoc, "changenameOK@");
                return ;
            }
        }
    }
    else if (keywords == "changepasswd")//修改密码
    {
        QString backdata = readdata.mid(keywords.length() + 1);
        for (int i = 0; i < USERSMAXNUM; i++)
        {
            if (m_usrdate[i].usrname == backdata.section(' ', 0, 0))
            {
                m_usrdate[i].passwd = backdata.section(' ', 1, 1);
                SaveUserData();
                tcpsoc->write("changepasswdOK@");//senddata(tcpsoc, "changepasswdOK@");
                return ;
            }
        }
    }
    else if (keywords == "searchfriend")//搜索好友
    {
        QString backdata = readdata.mid(keywords.length() + 1);
        for (int i = 0; i < USERSMAXNUM; i++)
        {
            if (m_usrdate[i].usrname == backdata.section(' ', 0, 0))
            {
                tcpsoc->write("searchfriendOK@");//senddata(tcpsoc, "searchfriendOK@");//搜索到好友
                return ;
            }
        }
        tcpsoc->write("unname@");//senddata(client_socket, "unname@");//未搜索到好友
        return ;
    }
    else if (keywords == "addfriend")//添加好友
    {
        QString backdata = readdata.mid(keywords.length() + 1);
        for (int i = 0; i < USERSMAXNUM; i++)
        {
            if (m_usrdate[i].usrname == backdata.section(' ', 0, 0))
            {
                for (int j = 0; j < USERSMAXNUM; j++)
                {
                    if (m_usrdate[j].usrname == backdata.section(' ', 1, 1))
                    {
                        m_usrdate[j].inform = m_usrdate[j].inform.append(QString("addfri %1 ").arg(m_usrdate[i].usrname));
                        tcpsoc->write("addfriendask@");//senddata(tcpsoc, "addfriendask@");//同意添加好友
                        return ;
                    }
                }
            }//tcpsoc->write();//
        }
    }
    else if (keywords == "addfriendOK")//同意添加好友
    {
        QString backdata = readdata.mid(keywords.length() + 1);
        for (int i = 0; i < USERSMAXNUM; i++)
        {
            if (m_usrdate[i].usrname == backdata.section(' ', 0, 0))
            {
                m_usrdate[i].friends = m_usrdate[i].friends.append(QString("%1 ").arg(backdata.section(' ', 1, 1)));
                for (int j = 0; j < USERSMAXNUM; j++)
                {
                    if (m_usrdate[j].usrname == backdata.section(' ', 1, 1))
                    {
                        m_usrdate[j].friends = m_usrdate[j].friends.append(QString("%1 ").arg(backdata.section(' ', 0, 0)));
                        SaveUserData();
                        tcpsoc->write("addfriendsuc@");//senddata(tcpsoc, "addfriendsuc@");//添加好友
                        return ;
                    }
                }
            }
        }
    }
    else if (keywords == "delfriend")//删除好友
    {
        QString backdata = readdata.mid(keywords.length() + 1);

        for (int i = 0; i < USERSMAXNUM; i++)
        {
            if (m_usrdate[i].usrname == backdata.section(' ', 0, 0))
            {
                int k = -1;
                int j;
                for (j = 0; m_usrdate[i].friends.section(' ', j, j) != ""; j++)
                {
                    if (m_usrdate[i].friends.section(' ', j, j) == backdata.section(' ', 1, 1))
                    {
                        k = j;
                    }
                }
                if (k > 0)
                {
                    QString fristr = QString("%1 %2").arg(m_usrdate[i].friends.section(' ', 0, k-1)).arg(m_usrdate[i].friends.section(' ', k+1, j));
                    m_usrdate[i].friends = fristr;
                }
                else if (k == 0)
                {
                    m_usrdate[i].friends = m_usrdate[i].friends.section(' ', 1, j);
                }
            }
            else if (m_usrdate[i].usrname == backdata.section(' ', 1, 1))
            {
                int k = -1;
                int j;
                for (j = 0; m_usrdate[i].friends.section(' ', j, j) != ""; j++)
                {
                    if (m_usrdate[i].friends.section(' ', j, j) == backdata.section(' ', 0, 0))
                    {
                        k = j;
                    }
                }
                if (k > 0)
                {
                    QString fristr = QString("%1 %2").arg(m_usrdate[i].friends.section(' ', 0, k-1)).arg(m_usrdate[i].friends.section(' ', k+1, j));
                    m_usrdate[i].friends = fristr;
                }
                else if (k == 0)
                {
                    m_usrdate[i].friends = m_usrdate[i].friends.section(' ', 1, j);
                }
            }
        }
        SaveUserData();
        tcpsoc->write("delfriendOK@");//senddata(tcpsoc, "delfriendOK@");//好友
        return ;
    }
    else if (keywords == "viewrooms")//查看房间
    {
        QString backdata = readdata.mid(keywords.length() + 1);
        int roomsnum = backdata.toInt();
        QString roomsstr = QString("rooms@");
        for (int i = 12*(roomsnum - 1); i < 12*roomsnum; i++)
        {
            if (m_nUserRooms[i][0] != 0 && m_nUserRooms[i][2] != 0)
            {
                roomsstr.append(QString("%1 %2 %3 ").arg(i+1).arg(m_usrdate[m_nUserRooms[i][0]].usrname).arg(m_usrdate[m_nUserRooms[i][2]].usrname));
            }
            else if (m_nUserRooms[i][0] != 0)
            {
                roomsstr.append(QString("%1 %2  ").arg(i+1).arg(m_usrdate[m_nUserRooms[i][0]].usrname));
            }
            else if (m_nUserRooms[i][2] != 0)
            {
                roomsstr.append(QString("%1  %2 ").arg(i+1).arg(m_usrdate[m_nUserRooms[i][2]].usrname));
            }
            else
            {
                roomsstr.append(QString("%1   ").arg(i+1));
            }
        }
        tcpsoc->write(roomsstr.toUtf8().data());//senddata(tcpsoc, roomsstr.toUtf8());//好友
        return ;
    }
    else if (keywords == "goroom")//进入房间
    {
        QString backdata = readdata.mid(keywords.length() + 1);
        int roomnum;
        roomnum = backdata.section(' ', 1, 1).toInt();
        int num;
        int place;
        for (place=0; place<30; place++)
        {
            if (place != 1 && place != 3)//跳过准备值
            {
                if (m_nUserRooms[roomnum-1][place] == 0)
                {
                    num = place;
                    break;
                }
            }
        }
        //printf("name = %s, num = %d\n", backdata.section(' ', 0, 0).toStdString().data(), num);
        for (int j = 0; j < USERSMAXNUM; j++)
        {
            if (m_usrdate[j].usrname == backdata.section(' ', 0, 0))
            {
                m_nUserRooms[roomnum-1][num] = j;//搜索到
                m_usrdate[j].room = roomnum;             //记录房间号
                break;
            }
        }

        QString roomsstr = QString("inroom@%1").arg(roomnum);
        if (m_nUserRooms[roomnum-1][0] != 0 && m_nUserRooms[roomnum-1][2] != 0)
        {
            roomsstr.append(QString(" %1 %2 %3 %4").arg(m_usrdate[m_nUserRooms[roomnum-1][0]].usrname).arg(m_nUserRooms[roomnum-1][1]).arg(m_usrdate[m_nUserRooms[roomnum-1][2]].usrname).arg(m_nUserRooms[roomnum-1][3]));
        }
        else if (m_nUserRooms[roomnum-1][0] != 0)
        {
            roomsstr.append(QString(" %1 %2  0").arg(m_usrdate[m_nUserRooms[roomnum-1][0]].usrname).arg(m_nUserRooms[roomnum-1][1]));
        }
        else if (m_nUserRooms[roomnum-1][2] != 0)
        {
            roomsstr.append(QString("  0 %1 %2").arg(m_usrdate[m_nUserRooms[roomnum-1][2]].usrname).arg(m_nUserRooms[roomnum-1][3]));
        }
        else
        {
            roomsstr.append(QString("  0  0"));
        }
        roomsstr.append(QString(" %1").arg(place));

        //qDebug() << "roomsstr = " << roomsstr;


        if (place <= 2)
        {
            tcpsoc->write(roomsstr.toUtf8().data());
        }
        else if (place > 3 && m_nUserRooms[roomnum-1][1] >= 1 && m_nUserRooms[roomnum-1][3] >= 1)//观众玩家,发送棋盘
        {
            QString chessboard;
            chessboard.append(" ");
            for (int i = 0; i < 15; i++)
            {
                for (int j = 0; j < 15; j++)
                {
                    QString strchess;
                    switch (m_cGobangRoom[roomnum - 1][i][j]) {
                        case 0: strchess = "0"; break;
                        case 1: strchess = "1"; break;
                        case 2: strchess = "2"; break;
                    }
                    chessboard.append(strchess);
                }
            }
            roomsstr.append(chessboard);
            //qDebug() << "inroom send:" << roomsstr;
            tcpsoc->write(roomsstr.toUtf8().data());
        }
        else
        {
            tcpsoc->write(roomsstr.toUtf8().data());
        }

        //如果房间内有其他玩家，告知其他玩家
        if (place == 0)
        {
            QString senddata = QString("usrinroom@%1 %2 1").arg(backdata.section(' ', 0, 0)).arg(roomnum);
            for (int j = 0; j < 30; j++)
            {
                if (j != 0 && j != 1 && j != 3)
                {
                    if (m_nUserRooms[roomnum - 1][j] != 0)
                    {
                        m_usrdate[m_nUserRooms[roomnum - 1][j]].socket->write(senddata.toUtf8().data());
                    }
                }
            }
        }
        else if (place == 2)
        {
            QString senddata = QString("usrinroom@%1 %2 2").arg(backdata.section(' ', 0, 0)).arg(roomnum);
            for (int j = 0; j < 30; j++)
            {
                if (j != 2 && j != 1 && j != 3)
                {
                    if (m_nUserRooms[roomnum - 1][j] != 0)
                    {
                        m_usrdate[m_nUserRooms[roomnum - 1][j]].socket->write(senddata.toUtf8().data());
                    }
                }
            }
        }
    }
    else if (keywords == "outroom")//退出房间
    {
        QString backdata = readdata.mid(keywords.length() + 1);
        for (int i = 0; i < USERSMAXNUM; i++)
        {
            if (m_usrdate[i].usrname == backdata.section(' ', 0, 0))
            {
                if(m_usrdate[i].room != 0)
                {
                    for (int j = 0; j < 30; j++)
                    {
                        if (j != 1 && j != 3)
                        {
                            if (m_nUserRooms[m_usrdate[i].room - 1][j] == m_usrdate[i].number)
                            {
                                m_nUserRooms[m_usrdate[i].room - 1][j] = 0;

                                //如果房间内有其他玩家，告知其他玩家
                                if (j == 0 || j == 2)
                                {
                                    if (j == 0)
                                    {
                                        QString senddata = QString("usroutroom@%1 %2 1").arg(backdata.section(' ', 0, 0)).arg(m_usrdate[i].room);
                                        for (int k = 0; k < 30; k++)
                                        {
                                            if (k != 0 && k != 1 && k != 3)
                                            {
                                                if (m_nUserRooms[m_usrdate[i].room - 1][k] != 0)
                                                {
                                                    m_usrdate[m_nUserRooms[m_usrdate[i].room - 1][k]].socket->write(senddata.toUtf8().data());
                                                }
                                            }
                                        }
                                        m_nUserRooms[m_usrdate[i].room - 1][1] = 0;
                                    }
                                    else if (j == 2)
                                    {
                                        QString senddata = QString("usroutroom@%1 %2 2").arg(backdata.section(' ', 0, 0)).arg(m_usrdate[i].room);
                                        for (int k = 0; k < 30; k++)
                                        {
                                            if (k != 2 && k != 1 && k != 3)
                                            {
                                                if (m_nUserRooms[m_usrdate[i].room - 1][k] != 0)
                                                {
                                                    m_usrdate[m_nUserRooms[m_usrdate[i].room - 1][k]].socket->write(senddata.toUtf8().data());
                                                }
                                            }
                                        }
                                        m_nUserRooms[m_usrdate[i].room - 1][3] = 0;
                                    }
                                }

                                m_usrdate[i].room = 0;
                                break;
                            }
                        }
                    }
                }
                m_usrdate[i].room = 0;
                break;
            }
        }
        tcpsoc->write("outroomOK@");
    }
    else if (keywords == "ready")//准备
    {
        QString backdata = readdata.mid(keywords.length() + 1);
        //qDebug() << "ready backdata:" << backdata;
        int room = backdata.section(' ', 0, 0).toInt();   // 房间号
        int readyint = backdata.section(' ', 2, 2).toInt(); // 准备值

        //qDebug() << "name:" << backdata.section(' ', 1, 1);
        //qDebug() << "name1:" << usrdat[UserRooms[room - 1][0]].usrname;
        //qDebug() << "name2:" << usrdat[UserRooms[room - 1][2]].usrname;

        if (m_usrdate[m_nUserRooms[room - 1][0]].usrname == backdata.section(' ', 1, 1))//如果用户为位置1
        {
            //qDebug() << "OK1\n";
            if (readyint == 1)//如果要黑棋
            {
                if (m_nUserRooms[room - 1][3] == 1)//如果已有黑棋
                {
                    readyint = 2;//改为白棋
                }
            }
            else if (readyint == 2)//如果要白棋
            {
                if (m_nUserRooms[room - 1][3] == 2)//如果已有白棋
                {
                    readyint = 1;//改为黑棋
                }
            }
            for (int i = 0; i < USERSMAXNUM; i++)
            {
                if (m_usrdate[i].usrname == backdata.section(' ', 1, 1))
                {
                    m_nUserRooms[room - 1][0] = i;
                    break;
                }
            }
            m_nUserRooms[room - 1][1] = readyint;
            if (m_nUserRooms[room - 1][2] != 0)
            {
                QString senddata = QString("usrready@%1 %2 %3").arg(room).arg(backdata.section(' ', 1, 1)).arg(readyint);
                m_usrdate[m_nUserRooms[room - 1][2]].socket->write(senddata.toUtf8().data());
            }
        }
        else if (m_usrdate[m_nUserRooms[room - 1][2]].usrname == backdata.section(' ', 1, 1))//如果用户为位置2
        {
            //qDebug() << "OK2\n";
            if (readyint == 1)
            {
                if (m_nUserRooms[room - 1][1] == 1)
                {
                    readyint = 2;
                }
            }
            else if (readyint == 2)
            {
                if (m_nUserRooms[room - 1][1] == 2)
                {
                    readyint = 1;
                }
            }
            for (int i = 0; i < USERSMAXNUM; i++)
            {
                if (m_usrdate[i].usrname == backdata.section(' ', 1, 1))
                {
                    m_nUserRooms[room - 1][2] = i;
                    break;
                }
            }
            m_nUserRooms[room - 1][3] = readyint;
            if (m_nUserRooms[room - 1][0] != 0)
            {
                QString senddata = QString("usrready@%1 %2 %3").arg(room).arg(backdata.section(' ', 1, 1)).arg(readyint);
                m_usrdate[m_nUserRooms[room - 1][0]].socket->write(senddata.toUtf8().data());
            }
        }
        QString senddata = QString("usrready@%1 %2 %3").arg(room).arg(backdata.section(' ', 1, 1)).arg(readyint);
        tcpsoc->write(senddata.toUtf8().data());
        //qDebug() << "ready senddata:" << senddata;
        for (int i = 4; i < 30; i++)
        {
            if (m_nUserRooms[room - 1][i] != 0)
            {
                m_usrdate[m_nUserRooms[room - 1][i]].socket->write(senddata.toUtf8().data());
            }
        }
        /*
        if (UserRooms[room - 1][1] != 0 && UserRooms[room - 1][3] != 0)// start game
        {
            for (int i = 0; i < 30; i++)
            {
                if (i != 1 && i != 3)
                {
                    senddata = QString("startgame@");
                    tcpsoc->write(senddata.toUtf8().data());
                }
            }
        }
        */
    }
    else if (keywords == "gobang")//玩家落子
    {
        QString backdata = readdata.mid(keywords.length() + 1);
        //qDebug() << "gobang = " << backdata;
        int room = backdata.section(' ', 0, 0).toInt();   // 房间号
        QString username = backdata.section(' ', 1, 1);
        backdata = backdata.section(' ', 2, 2);
        int piecesx = backdata.section(',', 0, 0).toInt();   // x
        int piecesy = backdata.section(',', 1, 1).toInt();   // y
        int nextstep = backdata.section(',', 2, 2).toInt();  // black or write  1/2
        m_cGobangRoom[room - 1][piecesx][piecesy] = nextstep;
        QString senddata = QString("tobang@%1 %2,%3,%4").arg(username).arg(piecesx).arg(piecesy).arg(nextstep);
        for (int i = 0; i < 30; i++)
        {
            if (i != 1 && i != 3)
            {
                if (m_nUserRooms[room - 1][i] != 0)
                {
                    if (m_usrdate[m_nUserRooms[room - 1][i]].usrname != username)
                    {
                        //qDebug() << "tobang = " << senddata;
                        m_usrdate[m_nUserRooms[room - 1][i]].socket->write(senddata.toUtf8().data());
                    }
                }
            }
        }
        //if (usrdat[UserRooms[room - 1][0]].usrname == username)
        //{
        //
        //}
    }
    else if (keywords == "overbang")//游戏结束
    {
        QString backdata = readdata.mid(keywords.length() + 1);
        int roomnum = backdata.section(' ', 0, 0).toInt();   // 房间号
        int winnum = backdata.section(' ', 1, 1).toInt();
        if (winnum != 3)
        {
            if (m_nUserRooms[roomnum - 1][1] == winnum)
            {
                m_usrdate[m_nUserRooms[roomnum - 1][0]].winnum++;
                m_usrdate[m_nUserRooms[roomnum - 1][2]].lossnum++;
                if (m_usrdate[m_nUserRooms[roomnum - 1][0]].score + 20 < m_usrdate[m_nUserRooms[roomnum - 1][2]].score)
                {
                    m_usrdate[m_nUserRooms[roomnum - 1][0]].score += 2;
                    m_usrdate[m_nUserRooms[roomnum - 1][2]].score -= 2;
                }
                else if (m_usrdate[m_nUserRooms[roomnum - 1][0]].score > m_usrdate[m_nUserRooms[roomnum - 1][2]].score + 20)
                {
                    m_usrdate[m_nUserRooms[roomnum - 1][0]].score++;
                }
                else if (m_usrdate[m_nUserRooms[roomnum - 1][0]].score > m_usrdate[m_nUserRooms[roomnum - 1][2]].score + 30)
                {
                }
                else
                {
                    m_usrdate[m_nUserRooms[roomnum - 1][0]].score++;
                    m_usrdate[m_nUserRooms[roomnum - 1][2]].score++;
                }
            }
            else
            {
                m_usrdate[m_nUserRooms[roomnum - 1][0]].lossnum++;
                m_usrdate[m_nUserRooms[roomnum - 1][2]].winnum++;
                if (m_usrdate[m_nUserRooms[roomnum - 1][2]].score + 20 < m_usrdate[m_nUserRooms[roomnum - 1][0]].score)
                {
                    m_usrdate[m_nUserRooms[roomnum - 1][2]].score += 2;
                    m_usrdate[m_nUserRooms[roomnum - 1][0]].score -= 2;
                }
                else if (m_usrdate[m_nUserRooms[roomnum - 1][2]].score > m_usrdate[m_nUserRooms[roomnum - 1][0]].score + 20)
                {
                    m_usrdate[m_nUserRooms[roomnum - 1][2]].score++;
                }
                else if (m_usrdate[m_nUserRooms[roomnum - 1][2]].score > m_usrdate[m_nUserRooms[roomnum - 1][0]].score + 30)
                {
                }
                else
                {
                    m_usrdate[m_nUserRooms[roomnum - 1][0]].score++;
                    m_usrdate[m_nUserRooms[roomnum - 1][2]].score++;
                }
            }
        }
        else
        {
            m_usrdate[m_nUserRooms[roomnum - 1][0]].drawnum++;
            m_usrdate[m_nUserRooms[roomnum - 1][2]].drawnum++;
        }
        m_usrdate[m_nUserRooms[roomnum - 1][0]].gobangnum++;
        m_usrdate[m_nUserRooms[roomnum - 1][2]].gobangnum++;
        m_nUserRooms[roomnum - 1][1] = 0;
        m_nUserRooms[roomnum - 1][3] = 0;
        for (int i = 0; i < 15; i++)
        {
            for (int j = 0; j < 15; j++)
            {
                m_cGobangRoom[roomnum - 1][i][j] = 0;
            }
        }
        SaveUserData();
        /*
        QString senddata = QString("usrready@%1 %2").arg(room).arg(winnum);
        for (int i = 0; i < 30; i++)
        {
            if (i != 1 && i != 3)
            {
                if (UserRooms[i][0] != 0)
                {
                    if (usrdat[UserRooms[i][0]].usrname != username)
                    {
                        usrdat[UserRooms[i][0]].socket->write(senddata.toUtf8().data());
                    }
                }
            }
        }
        */
    }
    else if (keywords == "undobang")//悔棋
    {
        QString backdata = readdata.mid(keywords.length() + 1);
        int roomnum = backdata.section(' ', 0, 0).toInt();   // 房间号
        QString username = backdata.section(' ', 1, 1);
        if (m_usrdate[m_nUserRooms[roomnum - 1][0]].usrname == username)
        {
            QString senddata = QString("advundo@");
            m_usrdate[m_nUserRooms[roomnum - 1][2]].socket->write(senddata.toUtf8().data());
        }
        else if (m_usrdate[m_nUserRooms[roomnum - 1][2]].usrname == username)
        {
            QString senddata = QString("advundo@");
            m_usrdate[m_nUserRooms[roomnum - 1][0]].socket->write(senddata.toUtf8().data());
        }
    }
    else if (keywords == "toundobang")// 同意/不同意悔棋
    {
        QString backdata = readdata.mid(keywords.length() + 1);
        //qDebug() << "toundobang read: " << backdata;
        int roomnum = backdata.section(' ', 0, 0).toInt();   // 房间号
        QString username = backdata.section(' ', 1, 1);      // 玩家名
        int dobang = backdata.section(' ', 2, 2).toInt();    // 是否同意
        //qDebug() << "user1:" << UserRooms[roomnum - 1][0] << "user2:" << UserRooms[roomnum][2];
        //qDebug() << "username1:" << usrdat[UserRooms[roomnum - 1][0]].usrname << "username2:" << usrdat[UserRooms[roomnum][2]].usrname;
        if (dobang == 0)  // 不同意悔棋
        {
            if (m_usrdate[m_nUserRooms[roomnum - 1][0]].usrname == username)
            {
                QString senddata = QString("advundobang@%1 %2 0").arg(roomnum).arg(username);
                m_usrdate[m_nUserRooms[roomnum - 1][2]].socket->write(senddata.toUtf8().data());
            }
            else if (m_usrdate[m_nUserRooms[roomnum - 1][2]].usrname == username)
            {
                QString senddata = QString("advundobang@%1 %2 0").arg(roomnum).arg(username);
                m_usrdate[m_nUserRooms[roomnum - 1][0]].socket->write(senddata.toUtf8().data());
            }
        }
        else
        {
            int tounnum = backdata.section(' ', 3, 3).toInt();
            QString tounusername;
            if (m_usrdate[m_nUserRooms[roomnum - 1][0]].usrname != username)  // 确认悔棋玩家名
            {
                tounusername = m_usrdate[m_nUserRooms[roomnum - 1][0]].usrname;
            }
            else
            {
                tounusername = m_usrdate[m_nUserRooms[roomnum - 1][2]].usrname;
            }
            QString senddata = QString("advundobang@%1 %2 1 %3 %4 %5 %6 %7").arg(roomnum).arg(tounusername).arg(tounnum)
                    .arg(backdata.section(' ', 4, 4).toInt()).arg(backdata.section(' ', 5, 5).toInt())
                    .arg(backdata.section(' ', 6, 6).toInt()).arg(backdata.section(' ', 7, 7).toInt());
            //qDebug() << "advundobang send:" << senddata;
            for (int i = 0; i < 30; i++)
            {
                if (i != 1 && i != 3)
                {
                    if (m_nUserRooms[roomnum - 1][i] != 0)
                    {
                        if (m_usrdate[m_nUserRooms[roomnum - 1][i]].usrname != username)
                        {
                            //qDebug() << "i = " << i;
                            m_usrdate[m_nUserRooms[roomnum - 1][i]].socket->write(senddata.toUtf8().data());
                        }
                    }
                }
            }

        }
    }
    else if (keywords == "drawbang")//平局
    {
        QString backdata = readdata.mid(keywords.length() + 1);
        int roomnum = backdata.section(' ', 0, 0).toInt();   // 房间号
        QString username = backdata.section(' ', 1, 1);
        if (m_usrdate[m_nUserRooms[roomnum - 1][0]].usrname == username)
        {
            QString senddata = QString("advdraw@");
            m_usrdate[m_nUserRooms[roomnum - 1][2]].socket->write(senddata.toUtf8().data());
        }
        else if (m_usrdate[m_nUserRooms[roomnum - 1][2]].usrname == username)
        {
            QString senddata = QString("advdraw@");
            m_usrdate[m_nUserRooms[roomnum - 1][0]].socket->write(senddata.toUtf8().data());
        }
        SaveUserData();
    }
    else if (keywords == "todrawbang") // 同意/不同意和棋
    {
        QString backdata = readdata.mid(keywords.length() + 1);
        //qDebug() << "todrawbang read:" << backdata;
        int roomnum = backdata.section(' ', 0, 0).toInt();   // 房间号
        QString username = backdata.section(' ', 1, 1);      // 玩家名
        int dobang = backdata.section(' ', 2, 2).toInt();    // 是否同意
        //qDebug() << "dobang = " << dobang;
        if (dobang == 0)  // 不同意和棋
        {
            if (m_usrdate[m_nUserRooms[roomnum - 1][0]].usrname == username)
            {
                QString senddata = QString("advdrawbang@%1 %2 0").arg(roomnum).arg(username);
                m_usrdate[m_nUserRooms[roomnum - 1][2]].socket->write(senddata.toUtf8().data());
                //qDebug() << "advdrawbang send:" << senddata;
            }
            else if (m_usrdate[m_nUserRooms[roomnum - 1][2]].usrname == username)
            {
                QString senddata = QString("advdrawbang@%1 %2 0").arg(roomnum).arg(username);
                m_usrdate[m_nUserRooms[roomnum - 1][0]].socket->write(senddata.toUtf8().data());
                //qDebug() << "advdrawbang send:" << senddata;
            }
        }
        else if (dobang == 1)
        {
            QString senddata = QString("advdrawbang@%1 %2 1").arg(roomnum).arg(username);
            for (int i = 0; i < 30; i++)
            {
                if (i != 1 && i != 3)
                {
                    if (m_nUserRooms[roomnum - 1][i] != 0)
                    {
                        m_usrdate[m_nUserRooms[roomnum - 1][i]].socket->write(senddata.toUtf8().data());
                        //qDebug() << "advdrawbang send:" << senddata;
                    }
                }
            }
        }
    }
    else if (keywords == "concedebang")// 认输
    {
        QString backdata = readdata.mid(keywords.length() + 1);
        int roomnum = backdata.section(' ', 0, 0).toInt();   // 房间号
        QString username = backdata.section(' ', 1, 1);
        int winnum;
        if (m_usrdate[m_nUserRooms[roomnum - 1][0]].usrname == username)
        {
            if (m_nUserRooms[roomnum - 1][1] == 1)
            {
                winnum = 2;
            }
            else
            {
                winnum = 1;
            }
        }
        else if (m_usrdate[m_nUserRooms[roomnum - 1][2]].usrname == username)
        {
            if (m_nUserRooms[roomnum - 1][1] == 1)
            {
                winnum = 1;
            }
            else
            {
                winnum = 2;
            }
        }
        if (m_nUserRooms[roomnum - 1][1] == winnum)
        {
            m_usrdate[m_nUserRooms[roomnum - 1][0]].winnum++;
            m_usrdate[m_nUserRooms[roomnum - 1][2]].lossnum++;
            if (m_usrdate[m_nUserRooms[roomnum - 1][0]].score + 20 < m_usrdate[m_nUserRooms[roomnum - 1][2]].score)
            {
                m_usrdate[m_nUserRooms[roomnum - 1][0]].score += 2;
                m_usrdate[m_nUserRooms[roomnum - 1][2]].score -= 2;
            }
            else if (m_usrdate[m_nUserRooms[roomnum - 1][0]].score > m_usrdate[m_nUserRooms[roomnum - 1][2]].score + 20)
            {
                m_usrdate[m_nUserRooms[roomnum - 1][0]].score++;
            }
            else if (m_usrdate[m_nUserRooms[roomnum - 1][0]].score > m_usrdate[m_nUserRooms[roomnum - 1][2]].score + 30)
            {
            }
            else
            {
                m_usrdate[m_nUserRooms[roomnum - 1][0]].score++;
                m_usrdate[m_nUserRooms[roomnum - 1][2]].score++;
            }
        }
        else
        {
            m_usrdate[m_nUserRooms[roomnum - 1][0]].lossnum++;
            m_usrdate[m_nUserRooms[roomnum - 1][2]].winnum++;
            if (m_usrdate[m_nUserRooms[roomnum - 1][2]].score + 20 < m_usrdate[m_nUserRooms[roomnum - 1][0]].score)
            {
                m_usrdate[m_nUserRooms[roomnum - 1][2]].score += 2;
                m_usrdate[m_nUserRooms[roomnum - 1][0]].score -= 2;
            }
            else if (m_usrdate[m_nUserRooms[roomnum - 1][2]].score > m_usrdate[m_nUserRooms[roomnum - 1][0]].score + 20)
            {
                m_usrdate[m_nUserRooms[roomnum - 1][2]].score++;
            }
            else if (m_usrdate[m_nUserRooms[roomnum - 1][2]].score > m_usrdate[m_nUserRooms[roomnum - 1][0]].score + 30)
            {
            }
            else
            {
                m_usrdate[m_nUserRooms[roomnum - 1][0]].score++;
                m_usrdate[m_nUserRooms[roomnum - 1][2]].score++;
            }
        }
        m_usrdate[m_nUserRooms[roomnum - 1][0]].gobangnum++;
        m_usrdate[m_nUserRooms[roomnum - 1][2]].gobangnum++;
        m_nUserRooms[roomnum - 1][1] = 0;
        m_nUserRooms[roomnum - 1][3] = 0;
        for (int i = 0; i < 15; i++)
        {
            for (int j = 0; j < 15; j++)
            {
                m_cGobangRoom[roomnum - 1][i][j] = 0;
            }
        }
        QString senddata = QString("advconcedebang@%1").arg(username);
        for (int i = 0; i < 30; i++)
        {
            if (i != 1 && i != 3)
            {
                if (m_nUserRooms[roomnum - 1][i] != 0)
                {
                    m_usrdate[m_nUserRooms[roomnum - 1][i]].socket->write(senddata.toUtf8().data());
                }
            }
        }
        SaveUserData();
    }
}

void SaveUserData()
{
    QDomDocument doc;
    QDomProcessingInstruction xmlInstruction = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
    QDomComment comment = doc.createComment(QString::fromLocal8Bit("Gobang后台用户数据"));
    doc.appendChild(xmlInstruction);
    doc.appendChild(comment);

    QDomElement root = doc.createElement("root");
    root.setAttribute("Version", "1.0");
    doc.appendChild(root);

    QDomElement domuserdata = doc.createElement("userdata");
    root.appendChild(domuserdata);

    for (int i = 0; i < USERSMAXNUM; i++)
    {
        if (!m_usrdate[i].usrname.isNull())
        {
            QDomElement domuser = doc.createElement(QString("user_%1").arg(i));
            domuser.setAttribute("name", m_usrdate[i].usrname);
            qDebug() << "name:" << m_usrdate[i].usrname << ", passwd:" << m_usrdate[i].passwd;
            domuser.setAttribute("passwd", m_usrdate[i].passwd);
            domuser.setAttribute("score", QString("%1").arg(m_usrdate[i].score));
            domuser.setAttribute("friends", m_usrdate[i].friends);
            domuser.setAttribute("number", QString("%1").arg(m_usrdate[i].number));
            domuser.setAttribute("date", m_usrdate[i].date);
            domuser.setAttribute("gobangnum", QString("%1").arg(m_usrdate[i].gobangnum));
            domuser.setAttribute("winnum", QString("%1").arg(m_usrdate[i].winnum));
            domuser.setAttribute("lossnum", QString("%1").arg(m_usrdate[i].lossnum));
            domuser.setAttribute("drawnum", QString("%1").arg(m_usrdate[i].drawnum));
            domuser.setAttribute("inform", m_usrdate[i].inform);
            domuser.setAttribute("usrnumvalue", QString("%1").arg(m_usrdate[i].usrnumvalue));
            domuserdata.appendChild(domuser);
            //m_nUsrNums[i] = m_nUsrNum;
        }
    }
    QString strFile(USERXMLPATH);
    QFile file(strFile);
    if (file.open(QFile::WriteOnly | QFile::Text))
    {
        QTextStream out(&file);
        doc.save(out, QDomNode::EncodingFromDocument);
        file.close();
    }
}

void ReadUserData()
{
    QFile file(USERXMLPATH);
    if (!file.open(QFile::ReadOnly))
    {
        return ;
    }
    QDomDocument doc;
    if (!doc.setContent(&file))
    {
        file.close();
        return ;
    }

    file.close();

    QDomElement root = doc.documentElement();
    QDomNode node = root.firstChild();
    if (node.hasChildNodes())
    {
        QDomNodeList listnodes = node.childNodes();
        for (int i = 0; i < listnodes.count(); i++)
        {
            QDomNode usernode = listnodes.at(i);
            QString strnodename = usernode.nodeName();
            int nodenum = strnodename.mid(5).toInt();
            //qDebug() << "nodenum = " << nodenum;
            //m_nUsrNums[nodenum] = nodenum;
            if (usernode.hasAttributes())
            {
                QDomNamedNodeMap attrs = usernode.attributes();
                for (int j = 0; j < attrs.count(); j++)
                {
                    QDomNode n = attrs.item(j);
                    QString nodeName = n.nodeName();
                    QString nodeValue = n.nodeValue();
                    //qDebug() << "name = " << nodeValue;
                    if (nodeName == "name")
                    {
                        if (!nodeValue.isNull())
                        {
                            m_usrdate[nodenum].usrname = nodeValue;
                        }
                    }
                    else if (nodeName == "passwd")
                    {
                        if (!nodeValue.isNull())
                        {
                            m_usrdate[nodenum].passwd = nodeValue;
                        }
                    }
                    else if (nodeName == "score")
                    {
                        if (!nodeValue.isNull())
                        {
                            m_usrdate[nodenum].score = nodeValue.toInt();
                        }
                    }
                    else if (nodeName == "friends")
                    {
                        if (!nodeValue.isNull())
                        {
                            m_usrdate[nodenum].friends = nodeValue;
                        }
                    }
                    else if (nodeName == "number")
                    {
                        if (!nodeValue.isNull())
                        {
                            m_usrdate[nodenum].number = nodeValue.toInt();
                        }
                    }
                    else if (nodeName == "date")
                    {
                        if (!nodeValue.isNull())
                        {
                            m_usrdate[nodenum].date = nodeValue;
                        }
                    }
                    else if (nodeName == "gobangnum")
                    {
                        if (!nodeValue.isNull())
                        {
                            m_usrdate[nodenum].gobangnum = nodeValue.toInt();
                        }
                    }
                    else if (nodeName == "winnum")
                    {
                        if (!nodeValue.isNull())
                        {
                            m_usrdate[nodenum].winnum = nodeValue.toInt();
                        }
                    }
                    else if (nodeName == "lossnum")
                    {
                        if (!nodeValue.isNull())
                        {
                            m_usrdate[nodenum].lossnum = nodeValue.toInt();
                        }
                    }
                    else if (nodeName == "drawnum")
                    {
                        if (!nodeValue.isNull())
                        {
                            m_usrdate[nodenum].drawnum = nodeValue.toInt();
                        }
                    }
                    else if (nodeName == "inform")
                    {
                        if (!nodeValue.isNull())
                        {
                            m_usrdate[nodenum].inform = nodeValue;
                        }
                    }
                    else if (nodeName == "usrnumvalue")
                    {
                        if (!nodeValue.isNull())
                        {
                            m_usrdate[nodenum].usrnumvalue = nodeValue.toInt();
                            m_nUsrNums[m_usrdate[nodenum].usrnumvalue] = nodenum;
                            //qDebug() << "usrnum:" << m_usrdate[nodenum].usrnumvalue << ", nodenum:" << nodenum;
                            qDebug() << "i:" << m_usrdate[nodenum].usrnumvalue << "m_nUseNum:" << nodenum;
                        }
                    }
                }
            }
        }
    }
}
